package ru.ipccenter.managedBeans;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Calendar;
import ru.ipccenter.beans.HumanBeanLocal;
import ru.ipccenter.entities.Human;

import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.persistence.PersistenceException;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;

/**
 * MIPT
 * Autor: tkorchagin
 * Date: 08.04.14
 */
@ViewScoped
@ManagedBean(name = "authors")
public class AuthorsManagedBean implements Serializable {
        
    
	private static final Logger LOG = Logger.getLogger(AuthorsManagedBean.class.getCanonicalName());

	@EJB
	HumanBeanLocal humanDAO;

	private List<Human> humanList;

	public List<Human> getHumanList() {
            return humanList;
	}
        
        @PostConstruct
        public void init() {
            try {
                //if (humanDAO.findAll().isEmpty()) {
                    Human h = new Human();
                    h.setFirstName("Vasya");
                    h.setLastName("Pupkin");
                    h.setBirthDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));

                    humanDAO.create(h);
                //} 
            } catch (PersistenceException e) {
                LOG.severe(e.getMessage());
            }
            
            try {
                humanList = humanDAO.findAll();
            } catch (PersistenceException e) {
                LOG.severe(e.getMessage());
            }
        }
}
